// import Item from './components/Item';
'use client';
import Main from '@/components/main';
import { getUserFromCookie } from '@/lib/auth';
import { redirect } from 'next/navigation';
import Right from '@/components/Right';
import http, { baseUrl } from '@/lib/request';
import {
	ApiResponse,
	LoginRes,
	MessageBase,
	MessageData,
} from '@/types/interface';
import { Suspense, use, useCallback, useEffect, useRef } from 'react';
import HomeLayer from '@/components/homelayer';
import { addhistoryData } from '@/store/initSlice';
import { useAppDispatch } from '@/store/hooks';
import { setSesstion } from '@/lib/session';
interface Props {
	params: Promise<{ id: string }>;
}

// export default function Home() {
// 	return (
// 		<div className="x">
// 			{list.map((item, i) => {
// 				return <Item {...item} key={i}></Item>;
// 			})}
// 		</div>
// 	);
// }

const HomePage: React.FC<Props> = ({ params }) => {
	// const token = await getUserFromCookie();
	// console.log(token);
	// if (!token) redirect('/login');
	const { id } = use(params);
	const dispatch = useAppDispatch();
	const timerRef = useRef<NodeJS.Timeout | null>(null);
	// if (!user.roles.includes('admin')) redirect('/403');
	// console.log(params.id);
	/**
	 * 心跳检测与 token 刷新
	 */
	const checkHeart = () => {
		timerRef.current = setInterval(() => {
			http.get('/perf/session/heart');
			(
				http.get('/perf/user/refresh/token') as Promise<ApiResponse<LoginRes>>
			).then((res) => {
				if (res.code === 0 && res.data?.access_token) {
					setSesstion('token', res.data.access_token);
				}
			});
		}, 60 * 1000); // 每分钟执行一次
	};
	// 获取历史会话内容
	const fetchMessages = useCallback(
		async (id: string) => {
			const res = await http.get(`${baseUrl}/perf/session/messgaes/${id}`);
			const result = res as ApiResponse<MessageData>;
			dispatch(addhistoryData(result.data));
		},
		[dispatch],
	);
	useEffect(() => {
		console.log(id);
		id && fetchMessages(id);

		checkHeart();

		return () => {
			if (timerRef.current) clearInterval(timerRef.current);
		};
	}, []);
	return <Right></Right>;
};
export default HomePage;
